﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Covid.Server.Entities;
using Covid.Shared.Dtos;

namespace Covid.Server.Data
{
    /// <summary>
    /// 数据上下文类
    /// </summary>
    public class MyDbContext:DbContext
    {

        /// <summary>
        /// 写一个构造函数，传入MyDbContext，调用父类的构造函数
        /// </summary>
        /// <param name="options"></param>
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { 
        }


        /// <summary>
        /// 重写
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder) {

            //设置联合主键
            modelBuilder.Entity<DailyHealth>().HasKey(u => new
            {
                u.EmployeeId,
                u.Date
            });

            //设置1-n关联关系:WithMany
            modelBuilder.Entity<Employee>().HasOne<Department>().WithMany().HasForeignKey(u=>u.DepartmentId).OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<DailyHealth>().HasOne<Employee>().WithMany().HasForeignKey(u=>u.EmployeeId).OnDelete(DeleteBehavior.Cascade);

            //设置种子数据
            modelBuilder.Entity<Department>().HasData(
                new Department { Id = 1, Name = "研发部" },
                new Department { Id = 2, Name = "销售部" },
                new Department { Id = 3, Name = "采购部" }
           );

            modelBuilder.Entity<Employee>().HasData(
                    new Employee
                    {
                        Id = 1,
                        DepartmentId = 1,
                        No = "A001",
                        Name = "杰克",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2002, 1, 1),
                        Gender = Gender.男,
                    },
                    new Employee
                    {
                        Id = 2,
                        DepartmentId = 1,
                        No = "A002",
                        Name = "唐尼",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2002, 11, 1),
                        Gender = Gender.未标明,
                    },
                    new Employee
                    {
                        Id = 3,
                        DepartmentId = 2,
                        No = "A003",
                        Name = "麦克",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2002, 11, 21),
                        Gender = Gender.男,
                    },
                    new Employee
                    {
                        Id = 4,
                        DepartmentId = 3,
                        No = "A004",
                        Name = "小红",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2001, 4, 1),
                        Gender = Gender.女,
                    },
                    new Employee
                    {
                        Id = 5,
                        DepartmentId = 2,
                        No = "A005",
                        Name = "小明",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2003, 1, 1),
                        Gender = Gender.男,
                    },
                    new Employee
                    {
                        Id = 6,
                        DepartmentId = 3,
                        No = "A006",
                        Name = "小葱",
                        PictureUrl = "one.png",
                        BirthDate = new DateTime(2001, 1, 1),
                        Gender = Gender.男,
                    }
                );
        }

        //DbSet
        public DbSet<Department> Departments { get; set; }

        public DbSet<Employee> Employees { get; set; }

        public DbSet<DailyHealth> DailyHealths { get; set; }


    }
}
